*Create results*

clear

use "Data\dataset_with_singles.dta" 

gen ambition=.
replace ambition=1 if ambition_type_k_4_s==1
replace ambition=2 if ambition_type_k_4_s==4
replace ambition=3 if ambition_type_k_4_s==3
replace ambition=4 if ambition_type_k_4_s==2


//generate partners' ambition
by couple_id aar, sort: gen am_male = ambition if koen=="1" & relationship==1
by couple_id aar: gen am_female = ambition if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(am_male)
by couple_id aar: replace am_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(am_female)
by couple_id aar: replace am_female = maxeduc
drop maxeduc

//generate partners' educ
by couple_id aar, sort: gen educ_male = fined if koen=="1" & relationship==1
by couple_id aar: gen educ_female = fined if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(educ_male)
by couple_id aar: replace educ_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(educ_female)
by couple_id aar: replace educ_female = maxeduc
drop maxeduc

//generate partners' field
by couple_id aar, sort: gen field_male = educ_level_field_num if koen=="1" & relationship==1
by couple_id aar: gen field_female = educ_level_field_num if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(field_male)
by couple_id aar: replace field_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(field_female)
by couple_id aar: replace field_female = maxeduc
drop maxeduc


*Ambition
*first parts of 1980
forvalues i=1980(1)1980{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

}

*then rest of loop
forvalues i=1981(1)2018{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

*optimal lambda cf. paper with Bastian
sca xi1_`i'=((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[4,4]/((male_4_`i')^2)))*(male_4_`i'-male_4_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[3,3]/((male_3_`i')^2)))*(male_3_`i'-male_3_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[2,2]/((male_2_`i')^2)))*(male_2_`i'-male_2_1980)

sca xi2_`i'=((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[4,4]/((female_4_`i')^2)))*(female_4_`i'-female_4_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[3,3]/((female_3_`i')^2)))*(female_3_`i'-female_3_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[2,2]/((female_2_`i')^2)))*(female_2_`i'-female_2_1980)

gen temp=.
replace temp=0 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')<abs(xi2_`i')
replace temp=(xi1_`i'/(xi1_`i'-xi2_`i')) if sign(xi1_`i')!=sign(xi2_`i')
replace temp=1 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')>abs(xi2_`i')

sca lambda_`i'=temp
drop temp

*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_`i'*male_`m'_`i'+(1-lambda_`i')*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

*finish 1980 part

forvalues i=1980(1)1980{
	
*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_1981*male_`m'_`i'+(1-lambda_1981)*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

**Add population marignals incl. singles and diagonal pi's

forvalues i=1980(1)2018{
*marginals
sca male_1_`i'_marr=mat_`i'[1,1]+mat_`i'[1,2]+mat_`i'[1,3]+mat_`i'[1,4]
sca male_2_`i'_marr=mat_`i'[2,1]+mat_`i'[2,2]+mat_`i'[2,3]+mat_`i'[2,4]
sca male_3_`i'_marr=mat_`i'[3,1]+mat_`i'[3,2]+mat_`i'[3,3]+mat_`i'[3,4]
sca male_4_`i'_marr=mat_`i'[4,1]+mat_`i'[4,2]+mat_`i'[4,3]+mat_`i'[4,4]

sca female_1_`i'_marr=mat_`i'[1,1]+mat_`i'[2,1]+mat_`i'[3,1]+mat_`i'[4,1]
sca female_2_`i'_marr=mat_`i'[1,2]+mat_`i'[2,2]+mat_`i'[3,2]+mat_`i'[4,2]
sca female_3_`i'_marr=mat_`i'[1,3]+mat_`i'[2,3]+mat_`i'[3,3]+mat_`i'[4,3]
sca female_4_`i'_marr=mat_`i'[1,4]+mat_`i'[2,4]+mat_`i'[3,4]+mat_`i'[4,4]

qui tab ambition if koen=="1" & relationship==0 & aar==`i', matcell(am_single_m_`i')
sca male_1_`i'_sing=am_single_m_`i'[1,1]
sca male_2_`i'_sing=am_single_m_`i'[2,1]
sca male_3_`i'_sing=am_single_m_`i'[3,1]
sca male_4_`i'_sing=am_single_m_`i'[4,1]

qui tab ambition if koen=="2" & relationship==0 & aar==`i', matcell(am_single_f_`i')
sca female_1_`i'_sing=am_single_f_`i'[1,1]
sca female_2_`i'_sing=am_single_f_`i'[2,1]
sca female_3_`i'_sing=am_single_f_`i'[3,1]
sca female_4_`i'_sing=am_single_f_`i'[4,1]

sca am_pi_1_`i'=mat_`i'[1,1]/((male_1_`i'_sing*female_1_`i'_sing)^(1/2))
sca am_pi_2_`i'=mat_`i'[2,2]/((male_2_`i'_sing*female_2_`i'_sing)^(1/2))
sca am_pi_3_`i'=mat_`i'[3,3]/((male_3_`i'_sing*female_3_`i'_sing)^(1/2))
sca am_pi_4_`i'=mat_`i'[4,4]/((male_4_`i'_sing*female_4_`i'_sing)^(1/2))

}

**Add share of power couples
forvalues i=1980(1)2018{
sca power_couple_`i'=mat_`i'_2[4,4]
}


frame copy default temp
frame change temp
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen diag=.
forvalues i=1980(1)2018{
replace diag=diag_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen s_`m'_`f'=.
forvalues i=1980(1)2018{
replace s_`m'_`f'=s_`m'_`f'_`i' if aar==`i' 
}
}
}

gen lambda=.
replace lambda=lambda_1981 if aar==1980 
forvalues i=1981(1)2018{
replace lambda=lambda_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen weight_`m'_`f'=.
forvalues i=1980(1)2018{
replace weight_`m'_`f'=weight_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen S_`m'_`f'=.
forvalues i=1980(1)2018{
replace S_`m'_`f'=S_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
gen male_`m'=.
forvalues i=1980(1)2018{
replace male_`m'=male_`m'_`i' if aar==`i' 
}
}

forvalues f=1(1)4{
gen female_`f'=.
forvalues i=1980(1)2018{
replace female_`f'=female_`f'_`i' if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_marr=.
forvalues i=1980(1)2018{
replace male_`m'_marr=male_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_sing=.
forvalues i=1980(1)2018{
replace male_`m'_sing=male_`m'_`i'_sing if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_marr=.
forvalues i=1980(1)2018{
replace female_`m'_marr=female_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_sing=.
forvalues i=1980(1)2018{
replace female_`m'_sing=female_`m'_`i'_sing if aar==`i' 
}
}



forvalues m=1(1)4{
gen am_pi_`m'=.
forvalues i=1980(1)2018{
replace am_pi_`m'=am_pi_`m'_`i' if aar==`i' 
}
}


gen power_couple=.
forvalues i=1980(1)2018{
replace power_couple=power_couple_`i' if aar==`i' 
}


save "Results\fig_B1\results_ambition.dta", replace

*****

*educ
frame change default
frame drop temp

sca drop _all
mat drop _all

*first parts of 1980
forvalues i=1980(1)1980{
*table
qui tab educ_male educ_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

}

*then rest of loop
forvalues i=1981(1)2018{
*table
qui tab educ_male educ_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

*optimal lambda cf. paper with Bastian
sca xi1_`i'=((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[4,4]/((male_4_`i')^2)))*(male_4_`i'-male_4_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[3,3]/((male_3_`i')^2)))*(male_3_`i'-male_3_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[2,2]/((male_2_`i')^2)))*(male_2_`i'-male_2_1980)

sca xi2_`i'=((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[4,4]/((female_4_`i')^2)))*(female_4_`i'-female_4_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[3,3]/((female_3_`i')^2)))*(female_3_`i'-female_3_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[2,2]/((female_2_`i')^2)))*(female_2_`i'-female_2_1980)

gen temp=.
replace temp=0 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')<abs(xi2_`i')
replace temp=(xi1_`i'/(xi1_`i'-xi2_`i')) if sign(xi1_`i')!=sign(xi2_`i')
replace temp=1 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')>abs(xi2_`i')

sca lambda_`i'=temp
drop temp

*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_`i'*male_`m'_`i'+(1-lambda_`i')*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

*finish 1980 part

forvalues i=1980(1)1980{
	
*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_1981*male_`m'_`i'+(1-lambda_1981)*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

**Add population marignals incl. singles and diagonal pi's

forvalues i=1980(1)2018{
*marginals
sca male_1_`i'_marr=mat_`i'[1,1]+mat_`i'[1,2]+mat_`i'[1,3]+mat_`i'[1,4]
sca male_2_`i'_marr=mat_`i'[2,1]+mat_`i'[2,2]+mat_`i'[2,3]+mat_`i'[2,4]
sca male_3_`i'_marr=mat_`i'[3,1]+mat_`i'[3,2]+mat_`i'[3,3]+mat_`i'[3,4]
sca male_4_`i'_marr=mat_`i'[4,1]+mat_`i'[4,2]+mat_`i'[4,3]+mat_`i'[4,4]

sca female_1_`i'_marr=mat_`i'[1,1]+mat_`i'[2,1]+mat_`i'[3,1]+mat_`i'[4,1]
sca female_2_`i'_marr=mat_`i'[1,2]+mat_`i'[2,2]+mat_`i'[3,2]+mat_`i'[4,2]
sca female_3_`i'_marr=mat_`i'[1,3]+mat_`i'[2,3]+mat_`i'[3,3]+mat_`i'[4,3]
sca female_4_`i'_marr=mat_`i'[1,4]+mat_`i'[2,4]+mat_`i'[3,4]+mat_`i'[4,4]

qui tab fined if koen=="1" & relationship==0 & aar==`i', matcell(am_single_m_`i') /*kept the am_ name of matrix*/
sca male_1_`i'_sing=am_single_m_`i'[1,1]
sca male_2_`i'_sing=am_single_m_`i'[2,1]
sca male_3_`i'_sing=am_single_m_`i'[3,1]
sca male_4_`i'_sing=am_single_m_`i'[4,1]

qui tab fined if koen=="2" & relationship==0 & aar==`i', matcell(am_single_f_`i') /*kept the am_ name of matrix*/
sca female_1_`i'_sing=am_single_f_`i'[1,1]
sca female_2_`i'_sing=am_single_f_`i'[2,1]
sca female_3_`i'_sing=am_single_f_`i'[3,1]
sca female_4_`i'_sing=am_single_f_`i'[4,1]

sca am_pi_1_`i'=mat_`i'[1,1]/((male_1_`i'_sing*female_1_`i'_sing)^(1/2)) /*kept the am_ name of scalars*/
sca am_pi_2_`i'=mat_`i'[2,2]/((male_2_`i'_sing*female_2_`i'_sing)^(1/2))
sca am_pi_3_`i'=mat_`i'[3,3]/((male_3_`i'_sing*female_3_`i'_sing)^(1/2))
sca am_pi_4_`i'=mat_`i'[4,4]/((male_4_`i'_sing*female_4_`i'_sing)^(1/2))

}

**Add share of power couples
forvalues i=1980(1)2018{
sca power_couple_`i'=mat_`i'_2[4,4]
}


frame copy default temp
frame change temp
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen diag=.
forvalues i=1980(1)2018{
replace diag=diag_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen s_`m'_`f'=.
forvalues i=1980(1)2018{
replace s_`m'_`f'=s_`m'_`f'_`i' if aar==`i' 
}
}
}

gen lambda=.
replace lambda=lambda_1981 if aar==1980 
forvalues i=1981(1)2018{
replace lambda=lambda_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen weight_`m'_`f'=.
forvalues i=1980(1)2018{
replace weight_`m'_`f'=weight_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen S_`m'_`f'=.
forvalues i=1980(1)2018{
replace S_`m'_`f'=S_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
gen male_`m'=.
forvalues i=1980(1)2018{
replace male_`m'=male_`m'_`i' if aar==`i' 
}
}

forvalues f=1(1)4{
gen female_`f'=.
forvalues i=1980(1)2018{
replace female_`f'=female_`f'_`i' if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_marr=.
forvalues i=1980(1)2018{
replace male_`m'_marr=male_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_sing=.
forvalues i=1980(1)2018{
replace male_`m'_sing=male_`m'_`i'_sing if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_marr=.
forvalues i=1980(1)2018{
replace female_`m'_marr=female_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_sing=.
forvalues i=1980(1)2018{
replace female_`m'_sing=female_`m'_`i'_sing if aar==`i' 
}
}



forvalues m=1(1)4{
gen am_pi_`m'=.
forvalues i=1980(1)2018{
replace am_pi_`m'=am_pi_`m'_`i' if aar==`i' 
}
}


gen power_couple=.
forvalues i=1980(1)2018{
replace power_couple=power_couple_`i' if aar==`i' 
}


save "Results\fig_B1\results_educ.dta", replace

*****

*fields
frame change default
frame drop temp

sca drop _all
mat drop _all

*first parts of 1980
forvalues i=1980(1)1980{
*table
qui tab field_male field_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]+mat_`i'_2[1,5]+mat_`i'_2[1,6]+mat_`i'_2[1,7]+mat_`i'_2[1,8]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]+mat_`i'_2[2,5]+mat_`i'_2[2,6]+mat_`i'_2[2,7]+mat_`i'_2[2,8]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]+mat_`i'_2[3,5]+mat_`i'_2[3,6]+mat_`i'_2[3,7]+mat_`i'_2[3,8]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]+mat_`i'_2[4,5]+mat_`i'_2[4,6]+mat_`i'_2[4,7]+mat_`i'_2[4,8]
sca male_5_`i'=mat_`i'_2[5,1]+mat_`i'_2[5,2]+mat_`i'_2[5,3]+mat_`i'_2[5,4]+mat_`i'_2[5,5]+mat_`i'_2[5,6]+mat_`i'_2[5,7]+mat_`i'_2[5,8]
sca male_6_`i'=mat_`i'_2[6,1]+mat_`i'_2[6,2]+mat_`i'_2[6,3]+mat_`i'_2[6,4]+mat_`i'_2[6,5]+mat_`i'_2[6,6]+mat_`i'_2[6,7]+mat_`i'_2[6,8]
sca male_7_`i'=mat_`i'_2[7,1]+mat_`i'_2[7,2]+mat_`i'_2[7,3]+mat_`i'_2[7,4]+mat_`i'_2[7,5]+mat_`i'_2[7,6]+mat_`i'_2[7,7]+mat_`i'_2[7,8]
sca male_8_`i'=mat_`i'_2[8,1]+mat_`i'_2[8,2]+mat_`i'_2[8,3]+mat_`i'_2[8,4]+mat_`i'_2[8,5]+mat_`i'_2[8,6]+mat_`i'_2[8,7]+mat_`i'_2[8,8]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]+mat_`i'_2[5,1]+mat_`i'_2[6,1]+mat_`i'_2[7,1]+mat_`i'_2[8,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]+mat_`i'_2[5,2]+mat_`i'_2[6,2]+mat_`i'_2[7,2]+mat_`i'_2[8,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]+mat_`i'_2[5,3]+mat_`i'_2[6,3]+mat_`i'_2[7,3]+mat_`i'_2[8,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]+mat_`i'_2[5,4]+mat_`i'_2[6,4]+mat_`i'_2[7,4]+mat_`i'_2[8,4]
sca female_5_`i'=mat_`i'_2[1,5]+mat_`i'_2[2,5]+mat_`i'_2[3,5]+mat_`i'_2[4,5]+mat_`i'_2[5,5]+mat_`i'_2[6,5]+mat_`i'_2[7,5]+mat_`i'_2[8,5]
sca female_6_`i'=mat_`i'_2[1,6]+mat_`i'_2[2,6]+mat_`i'_2[3,6]+mat_`i'_2[4,6]+mat_`i'_2[5,6]+mat_`i'_2[6,6]+mat_`i'_2[7,6]+mat_`i'_2[8,6]
sca female_7_`i'=mat_`i'_2[1,7]+mat_`i'_2[2,7]+mat_`i'_2[3,7]+mat_`i'_2[4,7]+mat_`i'_2[5,7]+mat_`i'_2[6,7]+mat_`i'_2[7,7]+mat_`i'_2[8,7]
sca female_8_`i'=mat_`i'_2[1,8]+mat_`i'_2[2,8]+mat_`i'_2[3,8]+mat_`i'_2[4,8]+mat_`i'_2[5,8]+mat_`i'_2[6,8]+mat_`i'_2[7,8]+mat_`i'_2[8,8]

*unweighted likelihood indices
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

}

*then rest of loop
forvalues i=1981(1)2018{
*table
qui tab field_male field_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]+mat_`i'_2[1,5]+mat_`i'_2[1,6]+mat_`i'_2[1,7]+mat_`i'_2[1,8]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]+mat_`i'_2[2,5]+mat_`i'_2[2,6]+mat_`i'_2[2,7]+mat_`i'_2[2,8]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]+mat_`i'_2[3,5]+mat_`i'_2[3,6]+mat_`i'_2[3,7]+mat_`i'_2[3,8]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]+mat_`i'_2[4,5]+mat_`i'_2[4,6]+mat_`i'_2[4,7]+mat_`i'_2[4,8]
sca male_5_`i'=mat_`i'_2[5,1]+mat_`i'_2[5,2]+mat_`i'_2[5,3]+mat_`i'_2[5,4]+mat_`i'_2[5,5]+mat_`i'_2[5,6]+mat_`i'_2[5,7]+mat_`i'_2[5,8]
sca male_6_`i'=mat_`i'_2[6,1]+mat_`i'_2[6,2]+mat_`i'_2[6,3]+mat_`i'_2[6,4]+mat_`i'_2[6,5]+mat_`i'_2[6,6]+mat_`i'_2[6,7]+mat_`i'_2[6,8]
sca male_7_`i'=mat_`i'_2[7,1]+mat_`i'_2[7,2]+mat_`i'_2[7,3]+mat_`i'_2[7,4]+mat_`i'_2[7,5]+mat_`i'_2[7,6]+mat_`i'_2[7,7]+mat_`i'_2[7,8]
sca male_8_`i'=mat_`i'_2[8,1]+mat_`i'_2[8,2]+mat_`i'_2[8,3]+mat_`i'_2[8,4]+mat_`i'_2[8,5]+mat_`i'_2[8,6]+mat_`i'_2[8,7]+mat_`i'_2[8,8]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]+mat_`i'_2[5,1]+mat_`i'_2[6,1]+mat_`i'_2[7,1]+mat_`i'_2[8,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]+mat_`i'_2[5,2]+mat_`i'_2[6,2]+mat_`i'_2[7,2]+mat_`i'_2[8,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]+mat_`i'_2[5,3]+mat_`i'_2[6,3]+mat_`i'_2[7,3]+mat_`i'_2[8,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]+mat_`i'_2[5,4]+mat_`i'_2[6,4]+mat_`i'_2[7,4]+mat_`i'_2[8,4]
sca female_5_`i'=mat_`i'_2[1,5]+mat_`i'_2[2,5]+mat_`i'_2[3,5]+mat_`i'_2[4,5]+mat_`i'_2[5,5]+mat_`i'_2[6,5]+mat_`i'_2[7,5]+mat_`i'_2[8,5]
sca female_6_`i'=mat_`i'_2[1,6]+mat_`i'_2[2,6]+mat_`i'_2[3,6]+mat_`i'_2[4,6]+mat_`i'_2[5,6]+mat_`i'_2[6,6]+mat_`i'_2[7,6]+mat_`i'_2[8,6]
sca female_7_`i'=mat_`i'_2[1,7]+mat_`i'_2[2,7]+mat_`i'_2[3,7]+mat_`i'_2[4,7]+mat_`i'_2[5,7]+mat_`i'_2[6,7]+mat_`i'_2[7,7]+mat_`i'_2[8,7]
sca female_8_`i'=mat_`i'_2[1,8]+mat_`i'_2[2,8]+mat_`i'_2[3,8]+mat_`i'_2[4,8]+mat_`i'_2[5,8]+mat_`i'_2[6,8]+mat_`i'_2[7,8]+mat_`i'_2[8,8]

*unweighted likelihood indices
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

*optimal lambda cf. paper with Bastian
sca xi1_`i'=((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[8,8]/((male_8_`i')^2)))*(male_8_`i'-male_8_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[7,7]/((male_7_`i')^2)))*(male_7_`i'-male_7_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[6,6]/((male_6_`i')^2)))*(male_6_`i'-male_6_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[5,5]/((male_5_`i')^2)))*(male_5_`i'-male_5_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[4,4]/((male_4_`i')^2)))*(male_4_`i'-male_4_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[3,3]/((male_3_`i')^2)))*(male_3_`i'-male_3_1980)+((mat_`i'_2[1,1]/((1-male_8_`i'-male_7_`i'-male_6_`i'-male_5_`i'-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[2,2]/((male_2_`i')^2)))*(male_2_`i'-male_2_1980)

sca xi2_`i'=((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[8,8]/((female_8_`i')^2)))*(female_8_`i'-female_8_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[7,7]/((female_7_`i')^2)))*(female_7_`i'-female_7_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[6,6]/((female_6_`i')^2)))*(female_6_`i'-female_6_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[5,5]/((female_5_`i')^2)))*(female_5_`i'-female_5_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[4,4]/((female_4_`i')^2)))*(female_4_`i'-female_4_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[3,3]/((female_3_`i')^2)))*(female_3_`i'-female_3_1980)+((mat_`i'_2[1,1]/((1-female_8_`i'-female_7_`i'-female_6_`i'-female_5_`i'-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[2,2]/((female_2_`i')^2)))*(female_2_`i'-female_2_1980)

gen temp=.
replace temp=0 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')<abs(xi2_`i')
replace temp=(xi1_`i'/(xi1_`i'-xi2_`i')) if sign(xi1_`i')!=sign(xi2_`i')
replace temp=1 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')>abs(xi2_`i')

sca lambda_`i'=temp
drop temp

*weights
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca weight_`m'_`f'_`i'=lambda_`i'*male_`m'_`i'+(1-lambda_`i')*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'+S_5_5_`i'+S_6_6_`i'+S_7_7_`i'+S_8_8_`i'

}

*finish 1980 part

forvalues i=1980(1)1980{
	
*weights
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca weight_`m'_`f'_`i'=lambda_1981*male_`m'_`i'+(1-lambda_1981)*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)8{
	forvalues f=1(1)8{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'+S_5_5_`i'+S_6_6_`i'+S_7_7_`i'+S_8_8_`i'

}

**Add population marignals incl. singles and diagonal pi's

forvalues i=1980(1)2018{
*marginals
sca male_1_`i'_marr=mat_`i'[1,1]+mat_`i'[1,2]+mat_`i'[1,3]+mat_`i'[1,4]+mat_`i'[1,5]+mat_`i'[1,6]+mat_`i'[1,7]+mat_`i'[1,8]
sca male_2_`i'_marr=mat_`i'[2,1]+mat_`i'[2,2]+mat_`i'[2,3]+mat_`i'[2,4]+mat_`i'[2,5]+mat_`i'[2,6]+mat_`i'[2,7]+mat_`i'[2,8]
sca male_3_`i'_marr=mat_`i'[3,1]+mat_`i'[3,2]+mat_`i'[3,3]+mat_`i'[3,4]+mat_`i'[3,5]+mat_`i'[3,6]+mat_`i'[3,7]+mat_`i'[3,8]
sca male_4_`i'_marr=mat_`i'[4,1]+mat_`i'[4,2]+mat_`i'[4,3]+mat_`i'[4,4]+mat_`i'[4,5]+mat_`i'[4,6]+mat_`i'[4,7]+mat_`i'[4,8]
sca male_5_`i'_marr=mat_`i'[5,1]+mat_`i'[5,2]+mat_`i'[5,3]+mat_`i'[5,4]+mat_`i'[5,5]+mat_`i'[5,6]+mat_`i'[5,7]+mat_`i'[5,8]
sca male_6_`i'_marr=mat_`i'[6,1]+mat_`i'[6,2]+mat_`i'[6,3]+mat_`i'[6,4]+mat_`i'[6,5]+mat_`i'[6,6]+mat_`i'[6,7]+mat_`i'[6,8]
sca male_7_`i'_marr=mat_`i'[7,1]+mat_`i'[7,2]+mat_`i'[7,3]+mat_`i'[7,4]+mat_`i'[7,5]+mat_`i'[7,6]+mat_`i'[7,7]+mat_`i'[7,8]
sca male_8_`i'_marr=mat_`i'[8,1]+mat_`i'[8,2]+mat_`i'[8,3]+mat_`i'[8,4]+mat_`i'[8,5]+mat_`i'[8,6]+mat_`i'[8,7]+mat_`i'[8,8]

sca female_1_`i'_marr=mat_`i'[1,1]+mat_`i'[2,1]+mat_`i'[3,1]+mat_`i'[4,1]+mat_`i'[5,1]+mat_`i'[6,1]+mat_`i'[7,1]+mat_`i'[8,1]
sca female_2_`i'_marr=mat_`i'[1,2]+mat_`i'[2,2]+mat_`i'[3,2]+mat_`i'[4,2]+mat_`i'[5,2]+mat_`i'[6,2]+mat_`i'[7,2]+mat_`i'[8,2]
sca female_3_`i'_marr=mat_`i'[1,3]+mat_`i'[2,3]+mat_`i'[3,3]+mat_`i'[4,3]+mat_`i'[5,3]+mat_`i'[6,3]+mat_`i'[7,3]+mat_`i'[8,3]
sca female_4_`i'_marr=mat_`i'[1,4]+mat_`i'[2,4]+mat_`i'[3,4]+mat_`i'[4,4]+mat_`i'[5,4]+mat_`i'[6,4]+mat_`i'[7,4]+mat_`i'[8,4]
sca female_5_`i'_marr=mat_`i'[1,5]+mat_`i'[2,5]+mat_`i'[3,5]+mat_`i'[4,5]+mat_`i'[5,5]+mat_`i'[6,5]+mat_`i'[7,5]+mat_`i'[8,5]
sca female_6_`i'_marr=mat_`i'[1,6]+mat_`i'[2,6]+mat_`i'[3,6]+mat_`i'[4,6]+mat_`i'[5,6]+mat_`i'[6,6]+mat_`i'[7,6]+mat_`i'[8,6]
sca female_7_`i'_marr=mat_`i'[1,7]+mat_`i'[2,7]+mat_`i'[3,7]+mat_`i'[4,7]+mat_`i'[5,7]+mat_`i'[6,7]+mat_`i'[7,7]+mat_`i'[8,7]
sca female_8_`i'_marr=mat_`i'[1,8]+mat_`i'[2,8]+mat_`i'[3,8]+mat_`i'[4,8]+mat_`i'[5,8]+mat_`i'[6,8]+mat_`i'[7,8]+mat_`i'[8,8]



qui tab educ_level_field_num if koen=="1" & relationship==0 & aar==`i', matcell(am_single_m_`i') /*kept the am_ name of matrix*/
sca male_1_`i'_sing=am_single_m_`i'[1,1]
sca male_2_`i'_sing=am_single_m_`i'[2,1]
sca male_3_`i'_sing=am_single_m_`i'[3,1]
sca male_4_`i'_sing=am_single_m_`i'[4,1]
sca male_5_`i'_sing=am_single_m_`i'[5,1]
sca male_6_`i'_sing=am_single_m_`i'[6,1]
sca male_7_`i'_sing=am_single_m_`i'[7,1]
sca male_8_`i'_sing=am_single_m_`i'[8,1]

qui tab educ_level_field_num if koen=="2" & relationship==0 & aar==`i', matcell(am_single_f_`i') /*kept the am_ name of matrix*/
sca female_1_`i'_sing=am_single_f_`i'[1,1]
sca female_2_`i'_sing=am_single_f_`i'[2,1]
sca female_3_`i'_sing=am_single_f_`i'[3,1]
sca female_4_`i'_sing=am_single_f_`i'[4,1]
sca female_5_`i'_sing=am_single_f_`i'[5,1]
sca female_6_`i'_sing=am_single_f_`i'[6,1]
sca female_7_`i'_sing=am_single_f_`i'[7,1]
sca female_8_`i'_sing=am_single_f_`i'[8,1]

sca am_pi_1_`i'=mat_`i'[1,1]/((male_1_`i'_sing*female_1_`i'_sing)^(1/2)) /*kept the am_ name of scalars*/
sca am_pi_2_`i'=mat_`i'[2,2]/((male_2_`i'_sing*female_2_`i'_sing)^(1/2))
sca am_pi_3_`i'=mat_`i'[3,3]/((male_3_`i'_sing*female_3_`i'_sing)^(1/2))
sca am_pi_4_`i'=mat_`i'[4,4]/((male_4_`i'_sing*female_4_`i'_sing)^(1/2))
sca am_pi_5_`i'=mat_`i'[5,5]/((male_5_`i'_sing*female_5_`i'_sing)^(1/2))
sca am_pi_6_`i'=mat_`i'[6,6]/((male_6_`i'_sing*female_6_`i'_sing)^(1/2))
sca am_pi_7_`i'=mat_`i'[7,7]/((male_7_`i'_sing*female_7_`i'_sing)^(1/2))
sca am_pi_8_`i'=mat_`i'[8,8]/((male_8_`i'_sing*female_8_`i'_sing)^(1/2))

}


frame copy default temp
frame change temp
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen diag=.
forvalues i=1980(1)2018{
replace diag=diag_`i' if aar==`i' 
}

forvalues m=1(1)8{
	forvalues f=1(1)8{
gen s_`m'_`f'=.
forvalues i=1980(1)2018{
replace s_`m'_`f'=s_`m'_`f'_`i' if aar==`i' 
}
}
}

gen lambda=.
replace lambda=lambda_1981 if aar==1980 
forvalues i=1981(1)2018{
replace lambda=lambda_`i' if aar==`i' 
}

forvalues m=1(1)8{
	forvalues f=1(1)8{
gen weight_`m'_`f'=.
forvalues i=1980(1)2018{
replace weight_`m'_`f'=weight_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)8{
	forvalues f=1(1)8{
gen S_`m'_`f'=.
forvalues i=1980(1)2018{
replace S_`m'_`f'=S_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)8{
gen male_`m'=.
forvalues i=1980(1)2018{
replace male_`m'=male_`m'_`i' if aar==`i' 
}
}

forvalues f=1(1)8{
gen female_`f'=.
forvalues i=1980(1)2018{
replace female_`f'=female_`f'_`i' if aar==`i' 
}
}

forvalues m=1(1)8{
gen male_`m'_marr=.
forvalues i=1980(1)2018{
replace male_`m'_marr=male_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)8{
gen male_`m'_sing=.
forvalues i=1980(1)2018{
replace male_`m'_sing=male_`m'_`i'_sing if aar==`i' 
}
}

forvalues m=1(1)8{
gen female_`m'_marr=.
forvalues i=1980(1)2018{
replace female_`m'_marr=female_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)8{
gen female_`m'_sing=.
forvalues i=1980(1)2018{
replace female_`m'_sing=female_`m'_`i'_sing if aar==`i' 
}
}



forvalues m=1(1)8{
gen am_pi_`m'=.
forvalues i=1980(1)2018{
replace am_pi_`m'=am_pi_`m'_`i' if aar==`i' 
}
}


save "Results\fig_B1\results_field.dta", replace

*****